package com.example.demo.tools.pianoTools;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class PianoKV {
    private Map<Double, Integer> pianoKV = new HashMap<Double, Integer>();

    public Map<Double, Integer> getPianoKV() {
        return pianoKV;
    }

    public PianoKV() {
        if(pianoKV.isEmpty()){
            Map<Double, Integer> kv = getKV();
            this.pianoKV = kv;
        }
    }

    public static Map<Double, Integer> getKV() {
        String strPath = "F:\\soundWork\\sound\\src\\main\\resources\\static\\music\\Piano\\mp3";
        Map<Double, Integer> m = new HashMap<Double, Integer>();
        ArrayList<Double> dou = new ArrayList<>();
        File dir = new File(strPath);
        File[] files = dir.listFiles();
        if (files != null) {
            for (int i = 0; i < files.length; i++) {
                String fileName = files[i].getName();
                if (files[i].isDirectory()) {
                    continue;
                } else if (fileName.endsWith("mp3")) {
                    String fileNameOld = files[i].getName();
                    String fileNameOldNew = fileNameOld.replace(".mp3", "");
                    double v = Double.parseDouble(fileNameOldNew);
                    dou.add(v);
                } else {
                    continue;
                }
            }
        }
        Collections.sort(dou);
        int a = 1;
        for (Double aDouble : dou) {
            m.put(aDouble, a);
            a ++;
        }
        return m;
    }
}
